<template>
  <div class="kinds">
    <div  class="buttonList">
      <div  class="buttonListStyle">
        <el-button type="primary" @click="addModel">新增</el-button>
        <el-button type="primary" @click="editModel">修改</el-button>
        <el-button type="primary" @click="delData">删除</el-button>
        <el-button type="primary" @click="getData">刷新</el-button>
      </div>
      <div class="menuArea">
      <el-tree 
        :data="dataList" 
        :props="defaultProps" 
        @node-click="handleNodeClick"
        highlight-current
      ></el-tree>
      </div>
    </div>
    <div  class="menuDetailArea">
      <div class="detailName">
        分类详情
      </div>
      <div class="pdd20">
          <el-form :inline="true">
            <el-form-item label="菜单名称">
              <el-input v-model="menuDetail.nodeName" readonly></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="menuDetail.remark" readonly></el-input>
            </el-form-item>
          </el-form>
      </div>
    </div>
     <!-- 新增弹框 -->
    <el-dialog
      title="新增菜单"
      :visible.sync="dialogVisibleAdd"
      width="400px"
      :close-on-click-modal="false"
      :show-close="false"
      :modal-append-to-body="false"
      >
      <div>
        <el-form :inline="true" :model="addMenu" :rules="rules" ref="addMenuFormRef">
          <el-form-item label="名称" prop="cateName" label-width="100px">
            <el-input v-model="addMenu.cateName" clearable></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark" label-width="100px">
           <el-input v-model="addMenu.remark" clearable></el-input>
          </el-form-item>        
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="sureAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹框 -->
    <el-dialog
      title="修改菜单"
      :visible.sync="dialogVisibleEdit"
      width="400px"
      :close-on-click-modal="false"
      :show-close="false"
      :modal-append-to-body="false"
      >
      <div>
        <el-form :inline="true" :model="editMenu" :rules="rules" ref="editMenuFormRef">
          <el-form-item label="名称" prop="cateName" label-width="100px">
            <el-input v-model="editMenu.cateName" clearable></el-input>
          </el-form-item>
           <el-form-item label="备注" prop="remark" label-width="100px">
           <el-input v-model="editMenu.remark" clearable></el-input>
          </el-form-item>       
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="sureEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
        dataList:[],
        defaultProps: {
          children: 'childNodes',
          label: 'nodeName'
        },
        menuDetail:{},
        isShow:true,
        menuFlag:true,
        editMenuSelectFlag:false,
        addMenu:{
          cateName:"",
          remark:""
        },
        editMenu:{},
        dialogVisibleAdd:false,
        dialogVisibleEdit:false,
        frontMenuCode:'',
        rules: {
          cateName: [{ required: true, message: "请输入名称", trigger: "blur" }],
        },
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    //获取树的信息
    getData(){
      //掉接口 查询菜单树接
      this.$axios.post(this.$store.state.url+'/cate/listCateTree').then(res => {
          if (res.data.code == 1) {
            this.dataList=res.data.data
          }else{
            this.$message({
              showClose: false,
              message: res.data.msg,
              type: 'error'
            });
          }
        })
      this.menuDetail={}
      this.addMenu={
          cateName:"",
          remark:""
      }
    },
    handleNodeClick(data) {
      this.addMenu.frontCateCode=data.nodeId
      data.cateName=data.attribute.cateName
      data.remark=data.attribute.remark
      this.menuDetail=data
    },
    //新增
    addModel() {
      this.dialogVisibleAdd = true;
      if (this.$refs["addMenuFormRef"]) {
        this.$refs["addMenuFormRef"].resetFields();
        this.menuFlag=true
      }
    },
    //确定新增
    sureAdd() {
      this.$refs["addMenuFormRef"].validate(valid => {
        if (valid) {
            this.$axios.post(this.$store.state.url+'/cate/addCate',this.addMenu).then(res => {
              if (res.data.code == 1) {
                   this.dialogVisibleAdd = false;
                   this.getData()
              }else{
                this.$message({
                  showClose: false,
                  message: res.data.msg,
                  type: 'error'
                });
              }
            })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //取消新增
    cancle(){
       this.dialogVisibleAdd = false;
       this.dialogVisibleEdit = false;
    },
    
    //修改
    editModel() {
      if(0<Object.keys(this.menuDetail).length){
        this.dialogVisibleEdit = true;
        this.editMenu=JSON.parse(JSON.stringify(this.menuDetail)).attribute
      }else{
        //提示不让修改
         this.$message.error('请选择一条数据进行修改');
      }
      if (this.$refs["editMenuFormRef"]) {
        this.$refs["editMenuFormRef"].resetFields();
      }
    },
    //确定修改
    sureEdit() {
      this.$refs["editMenuFormRef"].validate(valid => {
        if (valid) {
           this.$axios.post(this.$store.state.url+'/cate/updateCateBycode',this.editMenu).then(res => {
              if (res.data.code == 1) {
                   this.dialogVisibleEdit = false;
                   this.getData()
              }else{
                this.$message({
                  showClose: false,
                  message: res.data.msg,
                  type: 'error'
                });
              }
            })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //删除
    delData(){
      if(0<Object.keys(this.menuDetail).length){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //掉删除接口
           this.$axios.get(this.$store.state.url+'/cate/deleteCateBycode?cateCode='+this.menuDetail.attribute.cateCode).then(res => {
              if (res.data.code == 1) {
                    this.getData()
                    this.$message({
                      type: 'success',
                      message: '删除成功!'
                    });
              }else{
                this.$message({
                  showClose: false,
                  message: res.data.msg,
                  type: 'error'
                });
              }
            })  
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }else{
        //提示
        this.$message.error('请选择要删除的数据');
      }
    },
  }
};
</script>
<style scoped>
  .kinds{
    overflow: hidden;
    height: 100%;
    width: 100%;
  }
  .buttonList{
    width:25%;
    height:96%;
    float:left;
    border:1px solid #f2f2f2;
    padding:10px
  }
  .buttonListStyle{
    border-bottom:1px solid #f2f2f2;
    padding-bottom: 10px;
    text-align: center;     
  }
  .buttonListStyle>button{
  padding: 7px 10px !important;
  margin-left: 0 !important;
  width: 23%;
 }
  .menuDetailArea{
    width:70%;
    height:96%;
    float:left;
    border:1px solid #f2f2f2;
    padding:10px;
    border-left:0
  }
  .detailName{
    height:26px;
    line-height:26px;
    border-bottom:1px solid #f2f2f2;
    padding-bottom:10px
  }
  .menuArea{
    margin-top: 10px;
    height:calc(100% - 50px);
    overflow: auto;
  }
  .pdd20{
    padding: 20px
  }
</style>

<style>
  .kinds .el-button{
    padding: 5px !important;
  }
  .kinds .el-input__inner{
    width: 200px
  }
  .kinds .el-tree-node__content{
    height: 30px;
  }
  .kinds .el-tree-node__label{
  font-size: 16px !important
}
</style>
